package org.jeecg.modules.xxgx.leqiht.service.impl;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.modules.xxgx.fpgl.entity.CsXxFpxx;
import org.jeecg.modules.xxgx.fpgl.service.ICsXxFpxxService;
import org.jeecg.modules.xxgx.leqiht.service.ILeqiHtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LeqiHtServiceImpl implements ILeqiHtService {
    @Autowired
    private ICsXxFpxxService csXxFpxxService;
    /**
     * 乐企版式文件回填
     * @param paramJson
     * @return
     */
    @Override
    public Result<String> lqfpbswjjf(JSONObject paramJson) {
        log.info("乐企版式文件回填：{}", paramJson.toJSONString());
        String qqlsh = paramJson.getString("qqlsh");
        String xfsh = paramJson.getString("nsrsbh");
        JSONObject htxxJsonObject = JSONObject.parseObject(paramJson.getString("htxx"));
        if(htxxJsonObject.getIntValue("code") == 0) {
            JSONObject fpdzJsonObject =  htxxJsonObject.getJSONObject("data");
            String pdfdz = fpdzJsonObject.getString("pdfdz");
            String ofddz = fpdzJsonObject.getString("ofddz");
            String xmldz = fpdzJsonObject.getString("xmldz");

            CsXxFpxx csXxFpxx = csXxFpxxService.lambdaQuery().eq(CsXxFpxx::getDjbh, qqlsh).eq(CsXxFpxx::getXfsh,xfsh).last("limit 1").one();
            TenantContext.setTenant(String.valueOf(csXxFpxx.getTenantId()));
            if(csXxFpxx == null) {
                return Result.error("发票信息不存在");
            }
            CsXxFpxx updateFpxx = new CsXxFpxx();
            updateFpxx.setId(csXxFpxx.getId());
            updateFpxx.setPdfdz(pdfdz);
            updateFpxx.setOfddz(ofddz);
            updateFpxx.setXmldz(xmldz);
            csXxFpxxService.updateById(updateFpxx);
        }

        return Result.OK("成功");
    }
}
